package com.voyage.generator.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.voyage.common.constant.BaseController;
import com.voyage.common.constant.PageMO;
import com.voyage.common.constant.ResponseMO;
import com.voyage.generator.entity.CodeConfig;
import com.voyage.generator.model.ColumnInfo;
import com.voyage.generator.model.PrincipleMO;
import com.voyage.generator.service.GenConfigService;
import com.voyage.generator.service.GeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: kangb on 2021/10/9 19:14
 * @param:
 * @return:
 * @Description: 代码生成器配置管理
 */
@RestController
@RequestMapping("/generator")
@Api(tags = "代码生成器配置管理")
public class GenConfigController extends BaseController {

    private GenConfigService genConfigService;
    private GeneratorService generatorService;

    @Autowired
    public GenConfigController(GenConfigService genConfigService, GeneratorService generatorService) {
        this.genConfigService = genConfigService;
        this.generatorService = generatorService;
    }

    @ApiOperation(value = "分页展示")
    @PostMapping(value = "/list")
    public ResponseMO<IPage<CodeConfig>> list(@RequestBody PageMO<String> page) {
        IPage<CodeConfig> list = genConfigService.list(page);
        return success(list);
    }

    @ApiOperation(value = "获取表的列信息")
    @GetMapping(value = "/columns")
    public ResponseMO<List<ColumnInfo>> getAllColumnsByTableName(@RequestParam("tableName") String tableName) {
        List<ColumnInfo> list = generatorService.list(tableName);
        return success(list);

    }

    @GetMapping("/async")
    @ApiOperation(value = "同步表的元数据信息")
    public ResponseMO<Boolean> async() {
        return generatorService.async() ? success("同步成功") : error("同步失败");
    }

    @PostMapping("/execute")
    @ApiOperation(value = "代码生成")
    public ResponseMO<Boolean> execute(@Validated @RequestBody PrincipleMO principleMO) {
        return generatorService.execute(principleMO) ? success("代码生成成功") : error("代码生成失败");
    }

}
